﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Threading.Tasks;
using WCrawlerLib;

namespace WCrawler
{
    public partial class frmThreadPosts : System.Web.UI.Page
    {
        private int _indexThreadPost = 0; 

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void gridViewThreadPosts_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
                _indexThreadPost = gridViewThreadPosts.PageSize * gridViewThreadPosts.PageIndex + 1;
            else if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Create order numbers
                ((Label)e.Row.FindControl("lblNum0")).Text = (_indexThreadPost++).ToString();
            }
        }

        protected void ThreadPostLinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {

        }

        public void RunParallel(Forum objForum, List<int> indexList, int startIndex, int size)
        {
            //Run parallel
            Parallel.For(0, size, i =>
            {
                int rowIndex = indexList[i + startIndex];
                GridViewRow row = gridViewThreadPosts.Rows[rowIndex];
                objForum.UpdatePostNum((int)gridViewThreadPosts.DataKeys[row.RowIndex].Values[0], (int)gridViewThreadPosts.DataKeys[row.RowIndex].Values[1]);
            });

            //init paramaters after run parallel
            startIndex += size;
            if (startIndex + size > indexList.Count)
            {
                size = indexList.Count - startIndex;
                if (size > 0)
                    RunParallel(objForum, indexList, startIndex, size);
            }
            else
            {
                RunParallel(objForum, indexList, startIndex, size);
            }
        }

        protected void btnCheckThreadPosts_Click(object sender, EventArgs e)
        {
            //Get selected rows group by forumId
            Dictionary<int, List<int>> forumThreadDict = new Dictionary<int, List<int>>();
            foreach (GridViewRow row in gridViewThreadPosts.Rows)
            {
                //check postnum is empty or not
                Label lblPostNum = (Label)row.FindControl("lblPostNum");
                if (lblPostNum.Text == string.Empty) //if empty then add into list
                {
                    //Get forumId and add into dictionary
                    ForumThread objForumThread = new ForumThread();
                    objForumThread = objForumThread.GetForumThread((int)gridViewThreadPosts.DataKeys[row.RowIndex].Values[0]);
                    if (!forumThreadDict.Keys.Contains(objForumThread.ForumId))
                        forumThreadDict.Add(objForumThread.ForumId, new List<int>());

                    //add row index
                    forumThreadDict[objForumThread.ForumId].Add(row.RowIndex);
                }
            }

            //run by forum groups
            foreach (KeyValuePair<int, List<int>> threadList in forumThreadDict)
            {
                List<int> indexList = threadList.Value;
                //Run parallel with [size] forums every loop
                int startIndex = 0; //index of indexList
                int size = 20; //run maximum 50 forums in indexList for a parallel
                //Check if indexList < default size then update initialization
                if (indexList.Count < size)
                    size = indexList.Count;
                Forum objForum = new Forum();
                objForum = objForum.GetForum(threadList.Key);
                if (objForum.LoadForum() != null)
                {
                    //Start run parallel loops
                    RunParallel(objForum, indexList, startIndex, size);
                }
            }

            //Refresh gridview
            gridViewThreadPosts.DataBind();
        }
    }
}